package org.ww.cms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.ww.cms.bean.CrmBanner;
import org.ww.cms.service.CrmBannerService;
import org.ww.commonutils.R;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author ww
 * @since 2020-10-31
 */
@RestController
@RequestMapping("/cms/banneradmin")
//@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * 分页查询banner
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("page/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit){
        Page<CrmBanner> pageBanner=new Page<>(page,limit);
        crmBannerService.page(pageBanner,null);
        return R.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    /**
     * 添加banner
     * @param crmBanner
     * @return
     */
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    /**
     * 根据id修改banner
     * @param crmBanner
     * @return
     */
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    /**
     * 根据id查询banner
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public R getById(@PathVariable String id){
        CrmBanner banner = crmBannerService.getById(id);
        return R.ok().data("banner",banner);
    }

    /**
     * 根据id删除banner
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R deleteById(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }

}

